package com.yan.design.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.beans.Transient;
import java.util.Objects;

/**
 * @author shengren.yan
 * @create 2022-07-25
 */
@Data
@AllArgsConstructor
//@NoArgsConstructor
public class Uo implements Comparable{

    private String username;
//    @Transient 对象不能被反序列化
//    private transient Integer age;
    private Integer age;

    // 重新equals方法
    @Override
    public boolean equals(Object o) {
        if (this == o) {return true;}
        if (o == null || getClass() != o.getClass()){ return false;}
        Uo uo = (Uo) o;
        return Objects.equals(username, uo.username) &&
                Objects.equals(age, uo.age);
    }
    @Override
    public int hashCode() {
        return Objects.hash(username, age);
    }

    // 排序
    @Override
    public int compareTo(Object o) {
        if (o instanceof Uo) {
            Uo u = (Uo) o;
            // 先按年龄排，如果年龄一样按名字排 ，如果倒序 return - this.getAge().compareTo(u.getAge());
            if (this.getAge().equals(u.getAge())) {
                return this.getUsername().compareTo(u.getUsername());
            } else {
                return this.getAge().compareTo(u.getAge());
            }
        }
        return 0;
    }

}
